package cn.iocoder.yudao.module.yyzx.controller.admin.menu_food;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.menu_food.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.menu_food.Menu_foodDO;
import cn.iocoder.yudao.module.yyzx.service.menu_food.Menu_foodService;

@Tag(name = "管理后台 - 食物")
@RestController
@RequestMapping("/yyzx/menu_food")
@Validated
public class Menu_foodController {

    @Resource
    private Menu_foodService menu_foodService;

    @PostMapping("/create")
    @Operation(summary = "创建食物")
    @PreAuthorize("@ss.hasPermission('yyzx:menu_food:create')")
    public CommonResult<Integer> createMenu_food(@Valid @RequestBody Menu_foodSaveReqVO createReqVO) {
        return success(menu_foodService.createMenu_food(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新食物")
    @PreAuthorize("@ss.hasPermission('yyzx:menu_food:update')")
    public CommonResult<Boolean> updateMenu_food(@Valid @RequestBody Menu_foodSaveReqVO updateReqVO) {
        menu_foodService.updateMenu_food(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除食物")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:menu_food:delete')")
    public CommonResult<Boolean> deleteMenu_food(@RequestParam("id") Integer id) {
        menu_foodService.deleteMenu_food(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得食物")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:menu_food:query')")
    public CommonResult<Menu_foodRespVO> getMenu_food(@RequestParam("id") Integer id) {
        Menu_foodDO menu_food = menu_foodService.getMenu_food(id);
        return success(BeanUtils.toBean(menu_food, Menu_foodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得食物分页")
    @PreAuthorize("@ss.hasPermission('yyzx:menu_food:query')")
    public CommonResult<PageResult<Menu_foodRespVO>> getMenu_foodPage(@Valid Menu_foodPageReqVO pageReqVO) {
        PageResult<Menu_foodDO> pageResult = menu_foodService.getMenu_foodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, Menu_foodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出食物 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:menu_food:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMenu_foodExcel(@Valid Menu_foodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<Menu_foodDO> list = menu_foodService.getMenu_foodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "食物.xls", "数据", Menu_foodRespVO.class,
                        BeanUtils.toBean(list, Menu_foodRespVO.class));
    }

}